package cn.darkhorse.mesadmin.controller.device;

import cn.darkhorse.common.config.DbTypeConfig;
import cn.darkhorse.common.constant.CommonConstant;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description:  大屏
 * @author: mr.wang
 * @time: 2023/10/21 8:52
 */
@RestController
@RequestMapping("/external/large/screen")
public class LargeScreenController {
	
	@Resource
	private DbTypeConfig dbTypeConfig;

	/**
	 * 告警排行  3个月内数据
	 * @return
	 */
	@RequestMapping(value = "/alarm-ranking",method = RequestMethod.GET)
	public AjaxResult alarmRanking() {
		List<Map<String, Object>> maps = new ArrayList<>();
		String sql;
		if (dbTypeConfig.getType().equals(CommonConstant.DB_TYPE_SQLSERVER)) {
			 sql = "SELECT TOP 5 a.*,md.name FROM( SELECT device_id, COUNT ( 1) AS num FROM alarm_record_info WHERE alarm_date >= dateadd( mm,-3, GETDATE( ) ) GROUP BY device_id ) a ,mes_device md where a.device_id = md.id ORDER BY num DESC";
		}else if (dbTypeConfig.getType().equals(CommonConstant.DB_TYPE_MYSQL)){
			 sql = "SELECT a.*,md.name FROM( SELECT device_id, COUNT ( 1) AS num FROM alarm_record_info WHERE alarm_date >= DATE_SUB( CURDATE(), INTERVAL 3 DAY ) GROUP BY device_id ) a ,mes_device md where a.device_id = md.id ORDER BY num DESC limit 5";
		}else {
			throw new ServiceException(String.format("不支持【%s】数据库类型。", dbTypeConfig.getType()));
		}
		maps = SqlRunner.db().selectList(sql);
		return AjaxResult.success(maps);
	}

	/**
	 * 月告警统计 最近12个月
	 * @return
	 */
	@RequestMapping(value = "/monthly-alarm/statistics",method = RequestMethod.GET)
	public AjaxResult monthlyAlarmStatistics() {
		LinkedHashMap<String, Object> result = new LinkedHashMap<>();
		List<Map<String, Object>> maps = new ArrayList<>();
		String sql;
		if (dbTypeConfig.getType().equals(CommonConstant.DB_TYPE_SQLSERVER)) {
			 sql = "SELECT COUNT(1) as quantity , RIGHT('00'+CAST(MONTH(alarm_date) as VARCHAR(2)),2) as alarmDate FROM alarm_record_info WHERE alarm_date >= dateadd( YY,- 1, GETDATE() ) GROUP BY RIGHT('00'+CAST(MONTH(alarm_date) as VARCHAR(2)),2) ";
		}else if(dbTypeConfig.getType().equals(CommonConstant.DB_TYPE_MYSQL)){
			 sql = "SELECT COUNT( 1) AS quantity, RIGHT( '00' + CAST( MONTH ( alarm_date ) AS char), 2 ) AS alarmDate FROM alarm_record_info WHERE alarm_date >= DATE_SUB( CURDATE(), INTERVAL 1 year ) GROUP BY RIGHT ( '00' + CAST( MONTH ( alarm_date ) AS char), 2 )  ";
		}else {
			throw new ServiceException(String.format("不支持【%s】数据库类型。", dbTypeConfig.getType()));
		}
		maps = SqlRunner.db().selectList(sql);
		for(int i = 1 ;i<13;i++){
			String key = String.format("%02d", i);
			if(null != maps){
				Object  quantity = null;
				for(Map<String, Object> map:maps){
					String alarmDate = map.get("alarmDate").toString();
					if(StringUtils.isNotBlank(alarmDate)&&alarmDate.equals(key)){
						//result.put(key,map.get("quantity"));
						quantity = map.get("quantity");
						break;
					}
				}
				if(null != quantity){
					result.put(key,quantity);
				}else{
					result.put(String.format("%02d", i),"");
				}
			}else{
				result.put(String.format("%02d", i),"");
			}
		}
		return AjaxResult.success(result);
	}


	public static void main (String [] args){
		System.out.println(String.format("%02d", 10));
	}


	/**
	 * 设备告警 最近20条
	 * @return
	 */
	@RequestMapping(value = "/alarm/list",method = RequestMethod.GET)
	public AjaxResult list() {
		List<Map<String, Object>> maps = new ArrayList<>();
		String sql;
		if (dbTypeConfig.getType().equals(CommonConstant.DB_TYPE_SQLSERVER)) {
			sql = "select top 20 md.name,ari.alarm_message as alarmMessage,ari.alarm_date AS alarmDate  " +
					"from alarm_record_info ari,mes_device md where ari.device_id = md.id  ORDER BY alarm_date desc";
		}else if(dbTypeConfig.getType().equals(CommonConstant.DB_TYPE_MYSQL)){
			sql = "select md.name,ari.alarm_message as alarmMessage,ari.alarm_date AS alarmDate  " +
					"from alarm_record_info ari,mes_device md where ari.device_id = md.id  ORDER BY alarm_date desc limit 20";
		}else {
			throw new ServiceException(String.format("不支持【%s】数据库类型。", dbTypeConfig.getType()));
		}
		maps = SqlRunner.db().selectList(sql);
		return AjaxResult.success(maps);
	}


}
